/**
 * ReportGear(2011)
 */
package net.abbp.report.model.headerfooter;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.geom.Dimension2D;

import javax.swing.Icon;
import net.abbp.report.model.cell.style.FontStyle;
import net.abbp.swing.util.ImageUtils;

import com.reportgear.designer.pane.headerfooter.editor.TextElementEditor;
import com.reportgear.report.model.headerfooter.editor.ElementEditor;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * 页眉页脚元素-文本
 * 
 * @version 1.0 2011-4-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class TextHFElement extends AbstractHFElement {
    protected String text;
    protected FontStyle fontStyle;

    public TextHFElement() {
        this("", FontStyle.DEFAULT_STYLE);
    }

    public TextHFElement(String text, FontStyle fontStyle) {
        this.text = text;
        this.fontStyle = FontStyle.getInstance(fontStyle);
    }

    @Override
    public String getCaption() {
        return "文本";
    }

    @Override
    public Icon getIcon() {
        return ImageUtils.readIcon("dialog/headerfooter/text.png");
    }

    @Override
    public Dimension2D getSize() {
        String[] lines = this.text.split("\n");
        FontMetrics fontMetrics = GraphHelper.getFontMetrics(this.fontStyle);

        int w = 0;
        for (String line : lines) {
            w = Math.max(fontMetrics.stringWidth(line), w);
        }

        return new Dimension(w, fontMetrics.getHeight() * lines.length);
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setFontStyle(FontStyle fontStyle) {
        this.fontStyle = fontStyle;
    }

    public FontStyle getFontStyle() {
        return fontStyle;
    }

    // 编辑器
    private ElementEditor editor = new TextElementEditor();

    @Override
    public ElementEditor getEditor() {
        return this.editor;
    }
}
